package ca.polymtl.rubikcube.MainActivity;

import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import ca.polymtl.rubikcube.R;
import ca.polymtl.rubikcube.CameraActivity.CameraActivity;
import ca.polymtl.rubikcube.CubeActivity.CubeActivity;

public class MainActivity extends Activity {
	final private static String LOG_TAG = MainActivity.class.getSimpleName();

	static final private int CAMERA_ACTIVITY_REQUEST_CODE = 456;
	static final private int CUBE_ACTIVITY_REQUEST_CODE = 9292;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main_layout);
	}

	public void startWithPicture(View v) {

		Intent intent = new Intent(this, CameraActivity.class);

		startActivityForResult(intent, CAMERA_ACTIVITY_REQUEST_CODE);
	}

	public void startDefault(View v) {
		Intent intent = new Intent(this, CubeActivity.class);

		startActivity(intent);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		Log.d(LOG_TAG, "onActivityResult " + requestCode);

		switch (requestCode) {
		case CAMERA_ACTIVITY_REQUEST_CODE:
			afterCamera(requestCode, data);
			break;
		case CUBE_ACTIVITY_REQUEST_CODE:
			afterCube(requestCode, data);
			break;
		}
	}

	private void afterCamera(int resultCode, Intent data) {
		Log.d(LOG_TAG, "After camera");

		if (data != null) {
			Log.d(LOG_TAG, "Data is not null");
			Bundle extras = data.getExtras();
			Set<String> keySet = extras.keySet();
			for (String s : keySet) {
				Log.d(LOG_TAG, "    Returned extra " + s);
			}

			int[] colorsComplete = data.getIntArrayExtra("ca.polymtl.rubikcube.colorsComplete");
			if (colorsComplete != null) {
				Log.d(LOG_TAG, "    Colors found in intent");
				
				Intent intent = new Intent(this, CubeActivity.class);
				intent.putExtra("ca.polymtl.rubikcube.initialColors", colorsComplete);
				
				startActivity(intent);
				
			} else {
				Log.d(LOG_TAG, "    No colors in data intent");
			}
		} else {
			Log.d(LOG_TAG, "Data is null");
		}
	}

	private void afterCube(int requestCode, Intent data) {
		Log.d(LOG_TAG, "After cube");
	}
}
